define(function(require, exports, module) {
    'use strict';
    var rk = require('rk');
    require('../../su/xsySuInfo/widget');
    require('core/widgets/map/amap');
    require('core/widgets/itemsCRU/item');

    $.widget('rk.xsyOpportunityInfo', $.rk.xsySuInfo, {
        _params: {},
        _create: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            me._beIniting();
            me._loadData({}, function(result) {
                // result.uuid //控件的uuid
                // result.common //页面公共的数据
                // result.self//控件自己的数据
                elem.html(require('platform/widgets/crm/opportunity/xsyOpportunityInfo/widget.tpl'));
                me.detail = elem.find('.account-detail');
                me.amap = elem.find('.account-map').uniqueId();
                me.wideview = elem.find('a.account-map-wideview');
                me.detail.itemsinplace({
                    entities: result.self.entityData,
                    filtersMap: {
                        winRate: 'percent'
                    },
                    items: result.self.itemList,
                    savesuccess: function(evt, arg) {
                        // arg.element: 触发编辑的DOM元素
                        // arg.item: 编辑的字段
                        // arg.newValue: 保存成功的值
                        // arg.entity: 组件当前的全部数据
                        try {
                            //广播一个【外部】事件，控件以外会监听到
                            me._triggerOuterEvent('detailChanged', arg);
                        } catch (ex) {
                            console.log('trigger detailChanged error: ', ex);
                        }
                    }
                });

                // 收听【外部】控件广播而来的事件
                me._listenOuterEvent('afterPopupEdit', function(arg) {
                    me._loadData({}, function(result) {
                        var itemList = result.self.itemList;
                        var entityData = result.self.entityData;
                        itemList.forEach(function(item){
                            entityData[item.entryPropertyNameOnly] != 0 &&  (!entityData[item.entryPropertyNameOnly]) && (entityData[item.entryPropertyNameOnly] = null);
                        })
                        me.detail.itemsinplace('update', result.self.entityData);
                    });
                });
            }, function() {
                console.log('loading data error');
            });
        },
        _init: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;

        }
    });
});
